/**
 * 
 */
$(function() {
	$(".fied").show();
	var urlinfo = window.location.href;
	$.post('/getUserKeyword',{},
	function(data)
	{
		if(data.code==500){
			$(".fied").hide();
			alert('您的账户没有关键词哦！可以尝试手动输入！');
			return;
		}
		$("#username").text(data.username);
		if(data.getUserKeyword == null){
			$(".fied").hide();
			alert('您的账户没有关键词哦！可以尝试手动输入！');
			return;
		}
		var list = '';
		var map = data.getUserKeyword;
		for (var key in map) {  
			list += '<li class="one_tier" id=_'+key+' planId='+key+' isClick="0">\
					<img src="../img/add.png" class="add-img">\
			<span class="one_tier_name"><img src="../img/folder2.png" class="js-one_tier_name_img">'+ map[key] +'</span>';
	    }  
		list += '</li>';
		$("#tree_model").append(list);
		$(".fied").hide();
		
		$(document).on("click","#tree_model .one_tier",function(){
			var addImg = $(this).find(".add-img").attr("src");
			var folderImg = $(this).find(".one_tier_name").find(".js-one_tier_name_img").attr("src");
			var camid = $(this).attr("planId");
			var isClickPlan = $(this).attr("isClick");
			var add = '../img/add.png';
			var folder = "../img/folder2.png";
			var fh = $(this);
			if(isClickPlan == 0){
				$.ajax({
					url:"/getSession",
					type:"post",	
					data: JSON.stringify({"code":1,"param":camid}),
				    dataType:'json',
				    contentType:'application/json;charset=UTF-8',
				    success: function(data){
				    	var unitData="";
				    	unitData = data.identifier;
				    	if(unitData == "404"){
				    		alert("请求失败，请稍后重试！");
							return;
						}else{
							var html = '<ul class="two_tree">';
							for(var i=0;i<unitData.length;i++){
								for (var key in unitData[i]) { 
									html = html + '<li class="two_tier" uidName='+key+'-'+unitData[i][key]+'  unitId='+key+' id=_'+key+' isClick="0"><img src="../img/add.png" class="two_tier-img"><span>'+ unitData[i][key] +'</span></li>';
								}
							}
							html = html + '</ul>';
							$("#_"+camid).append(html);
							fh.find(".two_tree").slideToggle("slow");
							if(addImg == add ){
								fh.find(".add-img").attr("src","../img/subtract.png");
							} else {
								fh.find(".add-img").attr("src","../img/add.png");
							}
							
							if(folderImg == folder){
								fh.find(".one_tier_name").find(".js-one_tier_name_img").attr("src","../img/folder1.png");
							} else {
								fh.find(".one_tier_name").find(".js-one_tier_name_img").attr("src","../img/folder2.png");
							}
							fh.attr("isClick","1");
						}
				    }
				});	
			}else{
				fh.find(".two_tree").slideToggle("slow");
				if(addImg == add ){
					fh.find(".add-img").attr("src","../img/subtract.png");
				} else {
					fh.find(".add-img").attr("src","../img/add.png");
				}
				
				if(folderImg == folder){
					fh.find(".one_tier_name").find(".js-one_tier_name_img").attr("src","../img/folder1.png");
				} else {
					fh.find(".one_tier_name").find(".js-one_tier_name_img").attr("src","../img/folder2.png");
				}
			}
		});

		$(document).on("click","#tree_model .two_tier",function(event){
			event.stopPropagation(); 
			var unitId = $(this).attr("unitId");
			var unitMsg = $(this).attr("uidName");
			var addImg = $(this).parent().find(".two_tier-img").attr("src");
			var isClickUnit = $(this).attr("isClick");
			var add = '../img/add.png';
			var fh = $(this);
			if(isClickUnit == 0){
				$.ajax({
					url:"/getSession",
					type:"post",	
					data: JSON.stringify({"code":2,"param":unitId}),
				    dataType:'json',
				    contentType:'application/json;charset=UTF-8',
				    success: function(data){
				    	var kyData="";
				    	kyData = data.identifier;
				    	if(kyData == "404"){
							alert("请求失败，请稍后重试！");
							return;
						}else{
							var html = '<ul class="three_tree">';
							for(var i=0;i<kyData.length;i++){
								for (var key in kyData[i]) { 
									html = html + '<li class="three_tree_text" uuk='+unitMsg+'-'+kyData[i][key]+'><img src="../img/file.png" class="three_tree_text_img">'+ kyData[i][key] +'</li>';
								}
							}
							html = html + "</ul>";
							$("#_"+unitId).append(html);
							fh.children(".three_tree").slideToggle("slow");
							if (add == addImg) {
								fh.parent().find(".two_tier-img").eq(fh.index()).attr("src","../img/subtract.png");
							} else {
								fh.parent().find(".two_tier-img").eq(fh.index()).attr("src","../img/add.png");
							}
							fh.attr("isClick","1");
				    }
				}
				});
			}else{
				fh.children(".three_tree").slideToggle("slow");
				if (add == addImg) {
					fh.parent().find(".two_tier-img").eq(fh.index()).attr("src","../img/subtract.png");
				} else {
					fh.parent().find(".two_tier-img").eq(fh.index()).attr("src","../img/add.png");
				}
			}
			
		});
	
		$(document).on("click",".three_tree_text",function(event){
			event.stopPropagation();
			var text = $(this).text();
			var inputNum = $(".js-input-num").text();

		    if (inputNum < 20) {
		    	inputNum++;
		    	var uuk = $(this).attr("uuk");
		    	$(this).parents(".am-panel").parent().parent().next().append('<span uuk='+uuk+'>'+ text +'<a href="javascript:;" class="del_selected_key_word"><img src="../img/off_white.png" style="margin-left:5px;"></a></span>');
		    	$(".js-input-num").text(inputNum);
		    	$(this).attr("uuk");
		    }else{
		    	  alert("搜索词不能超过20个");
		    }
		})
		$(document).on("click",".del_selected_key_word",function(){
			var inputNum = $(".js-input-num").text();
			$(this).parent().remove();
			if (inputNum > 0) {
	            inputNum --;
	            $(".js-input-num").text(inputNum);
	        } 
		})
	}).error(function(){
		alert("请求失败！请重新从百度应用界面打开！");
		$(".fied").hide();
		window.location.href="https://fuwu.baidu.com/";
	});
	
	$("#acquire_creative").click(function() {
		var words = "";
	    if($('#tab1').attr('class').indexOf('active')>0){
	    	var amPaddingXs = $(".js-input-num").text();
		    if (amPaddingXs > 20) {
		    	alert("搜索词不能超过限定长度");
	            return;
	        }else if(amPaddingXs<=0){
	    	    alert('请输入关键词');
	    	    return;
	        }
		    $(this).parents(".content").find("#reception_selected span").each(function(){  
			    words = words + $(this).attr("uuk") + ',';
		    });	
		    words = "tab1"+"--"+words;
	    }else if($('#tab2').attr('class').indexOf('active')>0){ 
	        var kwNum =$(".js-input-num-key").text();
	        if (kwNum > 100) {
	            alert("搜索词不能超过限定长度");
	            return;
	        }else if(kwNum<=0){
	    	    alert('请输入关键词');
	     	    return;
	        }
	      	words = $('#manual_idea').val();
			if(words != ""){		
				words = words.replace(new RegExp("，","g"), ",");
			}			
			words = "tab2"+"--"+words;
	    }
	    if(words == undefined || words == ""){
			alert('请输入关键词');
			return;
	    }else{
			window.open(encodeURI('page/creative_list.html?keyword=' + words));
	    }			
	});
	
	//切换图标颜色
    $(".nav-tabs li").click(function(){
        $(".nav-tabs li a i").addClass("icon-white");
        $(this).find("a").find("i").removeClass("icon-white");
        if ($(this).index() == 0) {
            $(".select-key").show();
            $(".import-key").hide();
        } else {
            $(".import-key").show();
            $(".select-key").hide();
        }
    })
    
    //数值长度
    $(".js-am-padding-xs").on("input",function(){
    	 var inputNum = $(".js-input-num-key").text();
         if ($(this).val().length < 201) {
             $(".js-input-num-key").text($(this).val().length);
         } else {
             $(".js-input-num-key").text($(this).val().length);
             alert("超出长度了");
         }
    })
    
    function setIfOpen(val) {
	document.cookie = "ifOpen=" + val;
    }
    function getIfOpenVal(param) {
	var strCookie = document.cookie;
	var arrCookie = strCookie.split(";");
	for (var i = 0; i < arrCookie.length; i++) {
		var arr = arrCookie[i].split("=");
		if (arr[0] == param)
			return arr[1];
	}
	return null;
    }
     //收缩服务
	var ifOpen = getIfOpenVal("ifOpen");
	// index页面默认展开浮窗
	if(ifOpen == 0){
		$(".business-bridge-iframe").animate({
			left : "-230px",
		}, 0)
		$(".js-shrink-button").find("span").removeClass("control").text("服务指导");
		    $(".js-shrink-button").toggle(function(){
		        $(".business-bridge-iframe").animate({
		        	left : "0px",
					}, 1000)
					$(this).find("span").addClass("control").text("收起");
					setIfOpen(1);
				}, function() {
					$(".business-bridge-iframe").animate({
		            left:"-230px",
		        },1000)
		        $(this).find("span").removeClass("control").text("服务指导");
					setIfOpen(0);
				})
	}else{
		$(".js-shrink-button").toggle(function() {
			$(".business-bridge-iframe").animate({
				left : "-230px",
				}, 1000)
				$(this).find("span").removeClass("control").text("服务指导");
				setIfOpen(0);
		},function(){
			$(".business-bridge-iframe").animate({
				left:"0px",
		        },1000)
		        $(this).find("span").addClass("control").text("收起");
		        setIfOpen(1);
		})
	}
    
    // 关闭浮窗
    $(".js-off-white").click(function(){
        $(".business-bridge-iframe").hide();
    })
    
})
